<script>
	// #ifdef APP
	const websocketModule = uni.requireNativePlugin("kyzone-websocket");
	// #endif
	import config from "@/utils/config.js"

	export default {
		onLaunch:async function(e) {
			this.getall()
			// uni.clearStorageSync()
			// uni.clearStorage()
			// this.$store.commit("changeUserInfo","")
			
			// #ifdef APP
			let user = uni.getStorageSync("userInfo")
			let token = user.token
			if (token) {
				websocketModule.connect({
				     url: `${config.wss_host}?token=${token}`,
				     reconnect: true, // 是否重连
				     headers:[]
				})
				console.log('初始化socket')
			} 
			
			const WEBSOCKET_STATUS = {
			    CONNECTED: 1, // 已连接
			    CONNECTING: 0, // 链接中
			    RECONNECT: 2, // 重连中
			    DISCONNECTED: -1, // 已断开
			}
			plus.globalEvent.addEventListener('onConnectState', function(e) {
				const data = e.data;
				if (data == WEBSOCKET_STATUS.CONNECTED) {
					// console.log('成功')
					 //链接成功
				} else if (data == WEBSOCKET_STATUS.CONNECTING) {
					// console.log('连接中')
					//链接中
				} else if (data == WEBSOCKET_STATUS.DISCONNECTED) {
					// console.log('重连')
					// 重连中
				} else if (data == WEBSOCKET_STATUS.RECONNECT) {
					// console.log('断开')
					// 已断开
				}
			})
			// 链接异常
			plus.globalEvent.addEventListener('onErrorMsg', function(e) {
			    // console.log("错误信息:" + e.data);
			})
			
			plus.globalEvent.addEventListener('onMessage', function(e) {
				const data = JSON.parse(e.data);
				switch(data.type) {
					case 'init':
						//绑定UID
					break;
					case 'live':
						// 全局通知
						uni.$emit('onLive', data); 
					break;
					case 'ping':
						// 回个pong
						websocketModule.sendMessage(JSON.stringify({'type':'pong'}))
					break;
				}
				
				console.log('获得消息',data)
					
			})

			this.$nextTick(() => {
				// uni.unPreloadPage({
				// 	url: '/pages/live/index/index',
				// 	complete () {
				// 		uni.preloadPage({url: "/pages/live/index/index"})
				// 	}
				// })
			})
			
			// #endif
		},
		onShow: function() {
			console.log('App Show')
			
			// #ifdef APP
			let args= plus.runtime.arguments ||"";
			
			this.$nextTick(()=>{
				if(args){
					let srca = args.split("baodika://")[1]
					console.log(srca)
					uni.navigateTo({
						url:srca,
						complete: () => {
							// 取到参数值后，直接赋值清空，有些情况下，单独一个赋值并不能清空，请直接使用下面两条语句。
							plus.runtime.arguments = null;
							plus.runtime.arguments = "";
						}
					})
					
					// 处理args参数，如直达到某新页面等
				}
			})
			plus.push.addEventListener("click", function(msg) {
				console.log("click:" + JSON.stringify(msg));
				console.log(msg.payload);
				console.log(JSON.stringify(msg));
				//do something
			}, false);
			plus.push.addEventListener("receive", function(msg) {
				console.log("recevice:" ,msg)
				//do something
			}, false);
			// #endif
			
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			getall(){
				let that = this
				that.util.postRequest({
					url:"v1/public_/getConfig",data:{groups:{
							agreement:[
								"user_agreement","privacy_agreement","settled_agreement"
							],
							question:"*",
							goods:"*",
							lorry:"*",
							basic:"*"
						},
					}
				}).then(res=>{
					that.$store.commit("changeAgreement",res.data.agreement)
					that.$store.commit("changePublicConfig",res.data)
				})
				that.$getArea(0,0).then(async res=>{
					if(res.length>0){
						that.$getArea(res[0].id,1).then(async resa=>{
							that.$getArea(resa[0].id,2).then(async resb=>{
								
							})
						})
					}
				})
				that.util.getRequest({
					url:"v1/public_/getGasoline"
				}).then(res=>{
					that.$store.commit("changeGasonline",res.data)
				})
				that.util.postRequest({
					url:"v1/public_/getBrands",
					data:{
						"type":"lorry"
					}
				}).then(res=>{
					that.$store.commit("changeBrandList",res.data)
				})
				this.$getCarTypeList()
				that.$location.myGetLocation((res)=>{
					that.$getAreaByCoordinate(res.lng,res.lat,3)
				},false)
				that.util.getRequest({
					url:"v1/public_/getPayTypes",
				}).then(res=>{
					that.$store.commit("changePayTypeList",res.data)
				})
				that.$getUser()
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import url("@/static/css/public-scss.scss");
	view,input,text,textarea,input{
		box-sizing: border-box;
		line-height: 1;
	}
</style>
